package wexin.web.controll.verify;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PagedResourcesAssembler;
import org.springframework.hateoas.PagedResources;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import wexin.web.api.service.WxService;
import wexin.web.config.WxConfigStorage;
import wexin.web.jpa.domain.Article;
import wexin.web.jpa.domain.ArticleType;
import wexin.web.jpa.service.ArticleService;

@Controller
public class ApiVerifyController {
	
	private static final Logger logger = LoggerFactory.getLogger(ApiVerifyController.class);

	@Autowired
	private WxConfigStorage configStorage;

	@Autowired
	private WxService wxService;
	
	@Autowired
	private ArticleService articleService;
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/articles/{type}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
	@Transactional(readOnly = true)
    public HttpEntity<PagedResources<Article>> getAllArticle(
    		@PathVariable("type") String type,
    		Pageable pageable, 
    		PagedResourcesAssembler<Article> assembler) {
		ArticleType articleType = ArticleType.valueOf(type);
		Page<Article> articles = articleService.findByType(articleType, pageable);
		return new ResponseEntity(assembler.toResource(articles), HttpStatus.OK);
    }
	
	@ResponseBody
	@RequestMapping("/verify")
    public String greeting(HttpServletRequest request) {
		String signature = request.getParameter("signature");
	    String nonce = request.getParameter("nonce");
	    String timestamp = request.getParameter("timestamp");
	    String echostr = request.getParameter("echostr");
	    
	    System.out.println(signature + " " + nonce + " " + timestamp + " " + echostr);
	    if (!wxService.checkSignature(timestamp, nonce, signature)) {
	      // 消息签名不正确，说明不是公众平台发过来的消息
	    	logger.error("the signature not correct!!!");
	    	return echostr;
	    }
	    
	    return echostr;
    }
	
	@RequestMapping("/greeting")
    public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name, Model model) {
        model.addAttribute("name", name);
        if (name.equals("ffff")) {
        	return "test";
        }
        return "greeting";
    }
	
}
